<template>
  <div class="container">
    <div class="hd">
      <div class="hd-left">
        <el-form :model="searchForm" inline>
          <el-form-item>
            <el-input
              v-model="searchForm.name"
              placeholder="请输入审核人姓名"
              size="small"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="searchForm.canteen"
              placeholder="请选择食堂"
              size="small"
            >
              <el-option
                v-for="item in canteenOptions"
                :key="item.id"
                :label="item.name"
                :value="item.name"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="success" size="small" @click="search"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="hd-right">
        <TsButton
          perms="canteen-conf:post"
          label="添加"
          type="primary"
          size="small"
          @click="add"
        ></TsButton>
        <TsButton
          perms="canteen-conf:delete"
          label="删除"
          type="danger"
          size="small"
          @click="delMute"
        ></TsButton>
      </div>
    </div>
    <!--    表格区域-->
    <div
      class="table"
      style="height: calc(100% - 160px); margin-top: 20px; overflow-y: auto"
    >
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        :header-cell-style="{ background: '#FEF6F6' }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="locationAreaName" label="食堂名称" width="200">
        </el-table-column>
        <el-table-column
          prop="locationPrincipalName"
          label="审核人姓名"
          width="200"
        >
        </el-table-column>
        <el-table-column
          prop="locationPrincipalPhone"
          label="审核人电话"
          width="200"
        >
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="240px">
          <template #default="scope">
            <TsButton
              perms="canteen-conf:$id:put"
              style="color: #3cb6e6"
              type="text"
              size="mini"
              label="修改"
              @click="update(scope.row.locationId)"
            ></TsButton>
            <TsButton
              perms="canteen-conf:delete"
              style="color: #f30606"
              type="text"
              size="mini"
              label="删除"
              @click="del(scope.row.locationId)"
            ></TsButton>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    分页区域-->
    <el-pagination
      style="float: right; margin: 20px"
      background
      v-model:currentPage="pageNum"
      v-model:page-size="pageSize"
      layout=" prev, pager, next"
      :total="totalNum"
      @size-change="pageSizeChange"
      @current-change="pageCurrentChange"
    />
    <!-- 添加编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="userId ? '编辑' : '新增'"
      width="400px"
      :before-close="handleClose"
    >
      <el-form
        :model="userInfo"
        label-width="80px"
        ref="userRef"
        :rules="userRules"
      >
        <el-form-item label="食堂选择">
          <el-select
            v-model="userInfo.canteenId"
            placeholder="请选择食堂"
            size="small"
          >
            <el-option
              v-for="item in canteenOptions"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核人">
          <el-select
            v-model="userInfo.userId"
            placeholder="请选择审核人"
            size="small"
          >
            <el-option
              v-for="item in userOptions"
              :key="item.id"
              :value="item.id"
              :label="item.nickname"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input
            style="width: 212px"
            v-model.number="userInfo.phone"
            placeholder="请输入电话号码"
            size="small"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" size="small" @click="submit"
            >确定</el-button
          >
          <el-button @click="handleClose" size="small">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {
  canteenList,
  auditPage,
  userList,
  addUser,
  delUser,
  user,
  updateUser,
} from "@/api/canteen/auditUser";
import { checkPhone } from "@/utils/index";
import commonPageRepository from "@/composables/common-page-repository";
import { onMounted, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
const searchForm = ref({});
// 食堂数据
const canteenOptions = ref([]);
// 获取食堂数据
function getCanteenData() {
  canteenList().then((res) => {
    canteenOptions.value = res.data;
  });
}
//表格数据
const tableData = ref([]);
//分页查询数据
const pageNum = ref(1);
const pageSize = ref(100);
const totalNum = ref(0);

// 获取审核人数据
function getAuditPage() {
  auditPage({
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  }).then((res) => {
    tableData.value = res.data.content;
    totalNum.value = res.data.totalElements;
  });
}
//改变分页事件 const { pageSizeChange, pageCurrentChange } =
const { pageSizeChange, pageCurrentChange } = commonPageRepository(
  pageSize,
  pageNum,
  getAuditPage
);
// 搜索
function search() {
  auditPage({
    pageNum: pageNum.value,
    name: searchForm.value.name,
    pageSize: pageSize.value,
    locationAreaName: searchForm.value.canteen,
  }).then((res) => {
    tableData.value = res.data.content;
    totalNum.value = res.data.totalElements;
  });
}
// 对话框显示与隐藏
const dialogVisible = ref(false);
// 关闭对话框之前的回调函数
function handleClose() {
  dialogVisible.value = false;
  userId.value = "";
  userInfo.value = {};
}
const userInfo = ref({});
// 食堂审核人数据
const userOptions = ref([]);
// 添加
function add() {
  dialogVisible.value = true;
}
//获取食堂审核人数据
function getAuditUser() {
  //   获取食堂审核人数据
  userList().then((res) => {
    userOptions.value = res.data;
  });
}
//提交
function submit() {
  userRef.value.validate((valid) => {
    if (valid) {
      if (!userInfo.value.phone) {
        ElMessage.error("请输入手机号码");
        return;
      }

      if (!userId.value) {
        addUser({
          canteenId: userInfo.value.canteenId,
          phone: userInfo.value.phone,
          userId: userInfo.value.userId,
        }).then((res) => {
          if (res.code === 200) {
            ElMessage.success("添加成功");
            dialogVisible.value = false;
            getAuditPage();
            userId.value = "";
          }
        });
      } else {
        updateUser(userId.value, {
          canteenId: userInfo.value.canteenId,
          phone: userInfo.value.phone,
          userId: userInfo.value.userId,
        }).then((res) => {
          if (res.code === 200) {
            ElMessage.success("更新成功");
            dialogVisible.value = false;
            getAuditPage();
            userId.value = "";
          }
        });
      }
    }
  });
}
//多选数组
const muteArr = ref([]);
//多选操作
function handleSelectionChange(val) {
  muteArr.value = val.map((item) => item.locationId);
}
//多选删除
function delMute() {
  if (muteArr.value.length === 0) {
    ElMessage.warning("请选择要删除的数据");
    return;
  }
  delUser({
    ids: muteArr.value.join(","),
  }).then((res) => {
    if (res.code === 200) ElMessage.success("删除成功");
    getAuditPage();
  });
}
const userId = ref("");
//修改审核人信息
function update(id) {
  userId.value = id;
  dialogVisible.value = true;
  user(id).then((res) => {
    userInfo.value = res.data;
    userInfo.value.phone = res.data.locationPrincipalPhone * 1;
    userInfo.value.canteenId = res.data.locationAreaId * 1;
    userInfo.value.userId = res.data.locationPrincipalId;
  });
}
//删除信息
function del(id) {
  ElMessageBox.confirm("您确定要删除这条数据么?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delUser({
        ids: id,
      }).then((res) => {
        if (res.code === 200) {
          ElMessage.success("删除成功");
          getAuditPage();
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消操作",
      });
    });
}
const userRef = ref(null);
//表单验证规则
const userRules = ref({
  phone: [
    { required: true, message: "请输入手机号码", trigger: "blur" },
    { validator: checkPhone, trigger: "blur" },
  ],
});
onMounted(() => {
  getCanteenData();
  getAuditPage();
  getAuditUser();
});
</script>

<style lang="scss" scoped>
.container {
  height: calc(100vh - 200px);
  background: #ffffff;
  box-shadow: 0 0 16px rgba(96, 96, 96, 0.16);
  border-radius: 6px;
  padding: 20px;
  overflow-y: auto;
  margin-top: 20px;
  .hd {
    display: flex;
    align-content: center;
    justify-content: space-between;
    .hd-right {
      margin-top: 5px;
    }
  }
}
</style>
